/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import persistence.entity.Accounttransaction;
import persistence.entity.Customer;
import persistence.entity.Account;

/**
 *
 * @author Craig
 */
@Stateless
public class AccountTransactionHelper implements AccountTransactionHelperRemote {
    @EJB
    private AccounttransactionFacadeRemote accounttransactionFacade;
   
    @EJB
    private AccountFacadeRemote accountFacade;
    
    @EJB
    private CustomerFacadeRemote customerFacade;
    
    @EJB
    private BillerFacadeRemote billerFacade;

    @Override
    public List<Accounttransaction> getTransactionsForCustomer(int custID) {
        
        Customer c = customerFacade.find(custID);
        
        List<Account> accounts = accountFacade.getAccountList(custID, true);
        
        for (Account acc : accounts) {
            getTransactionsForAccount(acc);
            
        }
        
        return null;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<Accounttransaction> getTransactionsForAccount(Account acc) {
        
        List<Accounttransaction> allTransactions = accounttransactionFacade.findAll();
        List<Accounttransaction> accTransactions = new ArrayList<Accounttransaction>();
        
        int accID = acc.getAccountId();
        
        Account toAcc;
        Account fromAcc;
        for (Accounttransaction accTrans : allTransactions) {
            toAcc = accTrans.getTransToAccount();
            fromAcc = accTrans.getTransFromAccount();
            
            if ((toAcc != null && toAcc.getAccountId() == accID) ||    // is there a to account and do theirs ids match
                (fromAcc != null && fromAcc.getAccountId() == accID)) {// is there a from account and do theirs ids match
                accTransactions.add(accTrans);
            }
        }
        
        return accTransactions;
    }
    
    private Accounttransaction createTransaction(char type, int amount, String desc, Integer fromAcc, Integer toAcc) {
        boolean isValid = true;
        Account fromAccount = null;
        if (fromAcc != null) {
            fromAccount = accountFacade.find(fromAcc);
            //Balance check and disabled account check
            if (amount > fromAccount.getAccountBalance() || Boolean.parseBoolean(
                fromAccount.getIsdisabled())) {
                isValid = false;
            }
        }
        Account toAccount = null;
        if (toAcc != null) {
            toAccount = accountFacade.find(toAcc);
            //Disabled account check
            if (Boolean.parseBoolean(toAccount.getIsdisabled())) {
                isValid = false;
            }
        }
        // <= 0 check for amount
        if (amount <= 0) {
            //Are you stupid?
            return null;
        }
        if (isValid) {
            Accounttransaction at = accounttransactionFacade.createTransaction(type, amount, desc, fromAccount, toAccount);
            if (at != null) {   // update accounts
                if (fromAccount != null) {
                    fromAccount.setAccountBalance(fromAccount.getAccountBalance() - amount);
                    accountFacade.edit(fromAccount);
                }
                if (toAccount != null) {
                    toAccount.setAccountBalance(toAccount.getAccountBalance() + amount);
                    accountFacade.edit(toAccount);
                }
            }
            return at;
        }
        return null;
    }

    @Override
    public Accounttransaction createDeposit(int amount, String desc, int accID) {
        return createTransaction('D', amount, desc, null, accID);
    }

    @Override
    public Accounttransaction createWithdrawl(int amount, String desc, int accID) {
        return createTransaction('W', amount, desc, accID, null);
    }

    @Override
    public Accounttransaction createTransfer(int amount, String desc, int fromAccID, int toAccID) {
        return createTransaction('T', amount, desc, fromAccID, toAccID);
    }

    @Override
    public Accounttransaction createBillPayment(int amount, String desc, int custAccID, int billerID) {
        int billerAccID = billerFacade.getAccountID(billerID);
        return createTransaction('B', amount, desc, custAccID, billerAccID);
    }
}
